#!/bin/sh
#
# Build or run a docker image of twister
#

ACTION=${1:-run}
MODE=${2:---local}

IMAGE_NAME=twister
REMOTE_IMAGE_NAME=miguelfreitas/twister

set -e

shift

case $ACTION in

build)
  echo Building $IMAGE_NAME
  docker build -t $IMAGE_NAME .
  ;;

run)
  if [ "$MODE" = "--remote" ]; then
    shift
    IMAGE_NAME=$REMOTE_IMAGE_NAME
    echo Pulling new version of $IMAGE_NAME
    docker pull $IMAGE_NAME
  fi
  echo Running $IMAGE_NAME
  docker run -d -p 28332:28332 -v $HOME/.twister:/root/.twister $IMAGE_NAME "$@"
  echo "Twister should now be running at http://localhost:28332 (access with \"user\" / \"pwd\")"
  ;;

stop)
  CID=$(docker ps | grep twister | awk '{print $1}')
  if [ "$CID" ]; then
    echo Stopping container $CID
    docker stop $CID
    docker rm $CID
  fi
  ;;

*)
  echo Usage:
  echo "         $0 run [ARGS FOR TWISTERD..]"
  echo "         Run twister from a local docker build (default)"
  echo
  echo "         $0 run --remote [ARGS FOR TWISTERD..]"
  echo "         Run twister from the remote docker build at \"$REMOTE_IMAGE_NAME\""
  echo
  echo "         $0 build"
  echo "         Build a local docker image for twister"

  exit 2

esac
